/**
 * 作者：王波
 * @param {Object} flag 是否支持多图片选择
 * @param {Object} success 成功回调
 */
function PickImage(flag, success) {
	if(mui("#div_popover")[0]) {
		mui("#div_popover").popover("toggle");
	} else {
		var str = '<ul class="mui-table-view">' +
			'<li class="mui-table-view-cell"><a >从手机相册选择</a></li>' +
			'<li class="mui-table-view-cell"><a >拍照</a></li>' +
			'</ul>' +
			'<ul class="mui-table-view">' +
			'<li class="mui-table-view-cell"><a>取消</a></li>' +
			'</ul>';
		var _popover = document.createElement("div");
		_popover.id = "div_popover";
		_popover.className = "mui-popover mui-popover-action mui-popover-bottom";
		_popover.innerHTML = str;
		mui("body")[0].appendChild(_popover);
		mui("#div_popover").popover("toggle");
		//底部弹出框
		mui("#div_popover").on("tap", "a", function(e) {
			var text = this.innerHTML;
			mui("#div_popover").popover("toggle");
			switch(text) {
				case "取消":
					break;
				case "拍照":
					cameraImg();
					break;
				case "从手机相册选择":
					galleryImg();
					break;
			}
		});
	}

	//从照相机获取
	function cameraImg() {
		plus.camera.getCamera().captureImage(function(p) { //调用相机拍照的成功回调
			plus.io.resolveLocalFileSystemURL(p, function(entry) { //通过照片的路径获取文件
				var src = entry.toLocalURL();
				if(0 != src.toString().indexOf("file://")) {
					src = "file://" + src; //兼容以"file:"开头的情况
				}
				var result = new Array();
				result.push(src);
				success(result);
			});
		}, function(e) {
			//			mui.toast("打开照相机失败")
		});
	}

	//从相册选择获取
	function galleryImg() {
		plus.gallery.pick(function(pathArr) {
			var result = new Array();
			if(pathArr) {
				if(!flag) {
					result.push(pathArr)
				} else {
					for(var i in pathArr.files) {
						if(0 != pathArr.files[i].toString().indexOf("file://")) {
							pathArr.files[i] = "file://" + pathArr.files[i]; //兼容以"file:"开头的情况
						}
						result.push(pathArr.files[i]); //result是用来装本次的所选的图片组
					}
				}
				success(result);
			}

		}, function(e) {
			switch(e.code) {
				case -2:
					break;
				default:
					//					mui.toast(e.message)
					break;
			}
			console.log("================"+JSON.stringify(e))
		}, {
			filter: "image",
			animation:false,
			multiple: flag,
			maximum: 9 - imgNumber,
			system: false
		});
	}
}